package com.woniuxy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.controller.form.*;
import com.woniuxy.exception.DdException;
import com.woniuxy.exception.ParameterException;
import com.woniuxy.model.Channel;
import com.woniuxy.model.PawnShop;
import com.woniuxy.service.ChannelService;
import com.woniuxy.utils.NewCopyBean;
import com.woniuxy.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hujianying
 * @since 2022-03-25
 */
@Api(tags = "渠道商相关接口")
@RestController
@RequestMapping("/channel")
public class ChannelController {
    @Resource
    private ChannelService channelService;

    /**
     * 查询所有渠道商方法
     * @return
     */
    @ApiOperation("查找所有渠道商")
    @GetMapping("/findAll")
    public Result getAllChaannel(){
        return Result.success(200,"查询成功",channelService.list());
    }
    /**查询渠道商然后进行分页
     * 全查询分页
     * @param findAllChannelform
     * @return
     */
    @ApiOperation("分页查找渠道商")
    @GetMapping("/findAllChannel")
    public Result getAllChannelPage(FindAllChannelform findAllChannelform){
        return channelService.findchannelpage(findAllChannelform);
    }

    /**
     * 模糊查询渠道商分页
     * @param findAllChannelform
     * @return
     */
    @ApiOperation("查找模糊分页渠道商")
    @GetMapping("/findChannelbylike")
    public Result getChannelbylike(FindChannelBylikeform findAllChannelform){
        return channelService.findchannelpagelike(findAllChannelform);
    }

    /**
     *
     * @param delechannelform  就收多个要删除的渠道商对象 进行删除
     * @return
     * @throws DdException
     */
    @ApiOperation("删除渠道商")
    @PostMapping("/delchnnelById")
    public Result delechannelById(@RequestBody Delechannelform delechannelform) throws DdException {
        System.out.println();
        return channelService.delechannel(delechannelform);
    }

    /**
     * 对渠道商新增
     * @param addchannelform
     * @param bindingResult
     * @return
     * @throws DdException
     */
    @ApiOperation("新增渠道商")
    @PostMapping("/addChannle")
    public Result addChannel(@RequestBody @Valid Addchannelform addchannelform,
                             BindingResult bindingResult) throws DdException {
        if (bindingResult.hasErrors()) {
            List<ObjectError> allErrors = bindingResult.getAllErrors();
            String s="";
            for (ObjectError allError : allErrors) {
                s = allError.getDefaultMessage();
            }
            throw new ParameterException(500003, s);
        }
        return channelService.addchannel(addchannelform);
    }

    /**
     * 传回个一个新的渠道商对象
     * 根据id进去修改
     * @param updatechannelform
     * @return
     * @throws DdException
     */
    @ApiOperation("查找渠道商信息")
    @PostMapping("/updateChannle")
    public Result updateChannle(@RequestBody  Updatechannelform updatechannelform) throws DdException {
        Channel channelform = updatechannelform.getChannelform();

        channelService.update(channelform,new QueryWrapper<Channel>().eq("channel_id",channelform.getChannelId()));
        return Result.success(200,"修改成功",null);
    }
}

